//
//  ConvenientlyTakeDetailVC.m
//  ConvenientlyTake
//
//  Created by 张奇 on 2022/12/21.
//

#import "ConvenientlyTakeDetailVC.h"
#import "TGShowPhotoView.h"
#import "ConvenientlyTakeAPIRequest.h"
#import "ConvenieTakeDetailModel.h"

@interface ConvenientlyTakeDetailVC ()
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showPhotoViewMarginTop;
@property (weak, nonatomic) IBOutlet UIView *showPhotoTitleView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showPhotoTitleViewHeight;

@property (weak, nonatomic) IBOutlet TGShowPhotoView *showPhotoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showPhotoViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showPhotoViewMarginBottom;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showVideoViewMarginTop;
@property (weak, nonatomic) IBOutlet UIView *showVideoTitleView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showVideoTitleViewHeight;

@property (weak, nonatomic) IBOutlet TGShowPhotoView *showVideoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showVideoViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *showVideoViewMarginBottom;


@property (weak, nonatomic) IBOutlet UIView *feedBackBgView;

@property (weak, nonatomic) IBOutlet UILabel *feedbackLabel;
@property (weak, nonatomic) IBOutlet TGShowPhotoView *feedbackShowPhotoView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *feedbackShowPhotoViewHeight;

@property (nonatomic, strong) ConvenieTakeDetailModel *detailModel;

@end

@implementation ConvenientlyTakeDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    [self setupUI];
    [self requestDate];
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    [self setNavStyleWhiteAndBackBtn];
}

- (void)setupUI {
    self.feedBackBgView.hidden = YES;
    self.feedbackShowPhotoViewHeight.constant = 0;
}

- (void)requestDate {
    [TGProgressHUD showHUD:nil];
    [ConvenientlyTakeAPIRequest GETEventDetailWithEventId:R(self.eventId) success:^(TGResponseItem *responseItem) {
        [TGProgressHUD dismissMPHUD];
        if (responseItem.success) {
            
            self.detailModel = [ConvenieTakeDetailModel modelWithJSON:responseItem.data];
            [self configureData];
            
        }
    } fail:^(NSError *error) {
        
    }];
}

- (void)configureData {
    self.descLabel.text = R(self.detailModel.problemDescription);
    self.phoneLabel.text = R(self.detailModel.promoterPhone);
    self.addressLabel.text = R(self.detailModel.address);
    self.typeLabel.text = R(self.detailModel.eventTypeName);
    self.descLabel.text = R(self.detailModel.problemDescription);
    
    NSMutableArray *photoUrls = [NSMutableArray array];
    for (TGServiceFileModel *fileModel in self.detailModel.pictureList) {
        NSString *url = R(fileModel.filePath);
        if (![url containsString:@"http"]) {
            url = StringFormat(PhotoPrefixURL, R(fileModel.filePath));
        }
        NSString *urlStringUTF8 = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [photoUrls addObject:urlStringUTF8];
    }
    
    if (photoUrls.count > 0) {
        NSInteger count = ceil((photoUrls.count)/4.0);
        self.showPhotoView.imgUrlArray = photoUrls;
        self.showPhotoViewHeight.constant = PhotoCollectionViewHeight(count);
    } else {
        self.showPhotoViewMarginTop.constant = 0;
        self.showPhotoTitleView.hidden = YES;
        self.showPhotoTitleViewHeight.constant = 0;
        self.showPhotoViewHeight.constant = 0;
        self.showPhotoViewMarginBottom.constant = 0;
    }
    
    NSMutableArray *videoUrls = [NSMutableArray array];
    for (TGServiceFileModel *fileModel in self.detailModel.videoList) {
        NSString *url = R(fileModel.filePath);
        if (![url containsString:@"http"]) {
            url = StringFormat(PhotoPrefixURL, R(fileModel.filePath));
        }
        NSString *urlStringUTF8 = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [videoUrls addObject:urlStringUTF8];
    }
    
    if (videoUrls.count > 0) {
        NSInteger videoCount = ceil((videoUrls.count)/4.0);
        self.showVideoView.imgUrlArray = videoUrls;
        self.showVideoViewHeight.constant = PhotoCollectionViewHeight(videoCount);
    } else {
        self.showVideoViewMarginTop.constant = 0;
        self.showVideoTitleView.hidden = YES;
        self.showVideoTitleViewHeight.constant = 0;
        self.showVideoViewHeight.constant = 0;
        self.showVideoViewMarginBottom.constant = 0;
    }
    
    if (self.detailModel.showState) {
        self.feedBackBgView.hidden = NO;
        self.feedbackLabel.text = R(self.detailModel.disposeDesc);
        
        NSMutableArray *photoUrls = [NSMutableArray array];
        for (TGServiceFileModel *fileModel in self.detailModel.disposeFileList) {
            NSString *url = R(fileModel.filePath);
            if (![url containsString:@"http"]) {
                url = StringFormat(PhotoPrefixURL, R(fileModel.filePath));
            }
            NSString *urlStringUTF8 = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [photoUrls addObject:urlStringUTF8];
        }
        
        if (photoUrls.count > 0) {
            NSInteger count = ceil((photoUrls.count)/4.0);
            self.feedbackShowPhotoView.imgUrlArray = photoUrls;
            self.feedbackShowPhotoViewHeight.constant = PhotoCollectionViewHeight(count);
        } else {
            self.feedbackShowPhotoViewHeight.constant = 0;
        }
    } else {
        self.feedBackBgView.hidden = YES;
    }
    
}

@end
